package main

import (
	"fmt"
	"io/ioutil"
	"gopkg.in/yaml.v2"
)

//LoadConfig 加载 config.yaml 文件内容
func LoadConfig() (*UpgradeConf, error) {
	var conf *UpgradeConf
	yamlFile, err := ioutil.ReadFile("./config.yaml")
	if err != nil {
		return nil, err
	}
	if err = yaml.Unmarshal(yamlFile, &conf); err != nil {
		return nil, err
	}
	//if conf.NetworkName == "" {
	//	panic("load config file failed, no networkName")
	//}
	//if conf.WasabiNFSDir == "" {
	//	panic("load config file failed, no wasabiNFSDir")
	//}
	//if conf.NetworkID == 0 {
	//	panic("load config file failed, no networkID")
	//}
	//if conf.OldConfigPath == "" {
	//	panic("load config file failed, no oldConfigFilePath")
	//}
	//if conf.OldConfigPath == "" {
	//	panic("load config file failed, no oldConfigFilePath")
	//}
	//if conf.ChaincodePath == "" {
	//	panic("load config file failed ")
	//}
	conf.NewConfigPath = "./conf"
	return conf, nil
}

//UpgradeConf config.yaml对应结构体
type UpgradeConf struct {
	NetworkName   string `yaml:"networkName"`
	WasabiNFSDir  string `yaml:"wasabiNFSDir"`
	//FabricNFSDir  string `yaml:"fabricNFSDir"`
	NetworkID     uint64 `yaml:"networkID"`
	NewConfigPath string
	OldConfigPath string     `yaml:"oldConfigFilePath"`
	ChaincodePath string     `yaml:"chaincodePath"`
	ControlPort   []NodePort `yaml:"controlPort"`
}

//NodePort controlPort
type NodePort struct {
	Name string `yaml:"name"`
	Port int32  `yaml:"port"`
}

type Conf struct {
	Test []string `yaml:"array.test,flow"`
}

func main(){
	data := `array.test: ["val1", "val2", "val3"]`
	var conf Conf
	yaml.Unmarshal([]byte(data), &conf)

	data2, _ := yaml.Marshal(conf)
	fmt.Printf("%s\n", string(data2))
}